package fts_test.lib;

import java.io.*;
import java.nio.file.Paths;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.*;


public class lucene {

    private static Analyzer CreateAnalyzer() {
        //return new org.apache.lucene.analysis.standard.StandardAnalyzer();
        return new org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer();
    }

    public static String[] readbook(String path, String book) throws Exception {

        Analyzer a = CreateAnalyzer();
        Directory dir = FSDirectory.open(Paths.get(path));
        IndexWriterConfig iwc = new IndexWriterConfig(a);
        IndexWriter iw = new IndexWriter(dir, iwc);

        String[] ts = fileReader.read(book, (txt) -> {
            Document doc = new Document();
            doc.add(new TextField("info", txt, Field.Store.YES));
            iw.addDocument(doc);
            iw.commit();
            return 1;
        });

        iw.close();
        dir.close();

        return ts;

    }

    public static Object[] searchbook_engine(String path) throws Exception {
        Analyzer a = CreateAnalyzer();
        Directory dir = FSDirectory.open(Paths.get(path));
        IndexReader reader = DirectoryReader.open(dir);
        IndexSearcher is = new IndexSearcher(reader);
        return new Object[]{is, a, dir};
    }

    public static void searchbook_engine_close(Object[] args) throws Exception {
        ((Analyzer) args[1]).close();
        ((Directory) args[2]).close();
    }

    public static ConcurrentLinkedQueue<Document> searchbook(Object[] args, int count, int topn, String keywords) throws Exception {

        IndexSearcher is = (IndexSearcher) args[0];
        Analyzer a = (Analyzer) args[1];

        final ConcurrentLinkedQueue<Document> list = new ConcurrentLinkedQueue<Document>();
        keywords = "\"" + keywords + "\"";
        int r = fileReader.search(count, keywords, (txt) -> {
            int searchCount = 0;
            QueryParser parser = new QueryParser("info", a);
            Query query = parser.parse(txt);
            TopDocs topDocs = is.search(query, topn);
            ScoreDoc[] hits = topDocs.scoreDocs;
            for (ScoreDoc scoreDoc : hits) {
                int docid = scoreDoc.doc;
                Document doc = is.doc(docid);
                list.add(doc);
                searchCount++;
            }
            return searchCount;
        });
        return list;
    }

    public static void showKeywords(String content) throws IOException {
        Analyzer a = CreateAnalyzer();
        TokenStream tokenStream = a.tokenStream("content", new StringReader(content));
        tokenStream.addAttribute(CharTermAttribute.class);

        tokenStream.reset();

        System.out.println("content => ");
        while (tokenStream.incrementToken()) {
            CharTermAttribute charTermAttribute
                    = (CharTermAttribute) tokenStream.getAttribute(CharTermAttribute.class);
            System.out.print(charTermAttribute.toString() + " ");
        }
        System.out.println(".");
        tokenStream.end();
        tokenStream.close();
        a.close();
    }

    public static void _main(String[] args) throws Exception {

    }
}
